//class ToCPage
package baidu.reader.common.ui.toc 
{
	import baidu.lib.utils.*;
	import baidu.reader.common.*;
	import baidu.reader.common.model.*;
	import baidu.reader.common.model.VO.*;
	import baidu.reader.common.utils.*;
	import baidu.ui.constants.*;
	import baidu.ui.containers.*;
	import baidu.ui.controls.*;
	import baidu.ui.core.*;
	import baidu.ui.layout.*;
	import baidu.ui.managers.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	
	public class ToCPage extends AutoLayoutContainer
	{
		private var tab_delimiter:int;
		[Embed(source="assets/sprite187.swf")]
		private var ToCHead:Class;
		private var tocHead:Sprite;
		private var _backButton:Button;
		private var tocModel:TocDataModel;
		private var screenModel:ScreenModel;
		private var tabBox:HBox;
		private var tocContent:ScrollPane;
		private var tocPages:Vector.<DisplayObject>;
		private var tabItemTextFormat:TextFormat;
		private var tabItemOverTextFormat:TextFormat;
		private static const TOC_TAB_DELIMITER:int = 200;
		private static const LARGE_TOC_TAB_DELIMITER:int = 400;
		private static const LARGE_TOC_THRESHOLD:int = 1000;
		private var generateSingleContent:Function;
		
		public function ToCPage()
		{
			this.tabItemTextFormat = new TextFormat("宋体", 12, 10450001);
			this.tabItemOverTextFormat = new TextFormat("宋体", 12, 16777215);
			this.addTocHead();
			this.addBackButton();
			this.initModel();
			return;
		}
		
		private function initModel() : void
		{
			this.tocModel = InstanceManager.createSingletonInstance(TocDataModel);
			this.tocModel.addEventListener(Event.CHANGE, this.dataChangeHandler);
			this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
			this.screenModel.addEventListener(Event.RESIZE, this.screenResizeHandler);
			if (this.tocModel.tocData.length)
			{
				this.refreshToC();
			}
			return;
		}
		
		private function screenResizeHandler(event:Event) : void
		{
			this.refreshToC();
			return;
		}
		
		private function dataChangeHandler(event:Event) : void
		{
			this.refreshToC();
			return;
		}
		
		private function refreshToC() : void
		{
			var tocGroup:RadioButtonGroup;
			var _len:int;
			var tabLen:int;
			var data:Vector.<TocItemVO>;
			var tempItemVector:Vector.<DisplayObject>;
			var i:int;
			generateSingleContent = function () : void
			{
				var _loc_8:String = null;
				var _loc_9:ToCItemButton = null;
				var _loc_10:AutoLayoutContainer = null;
				var _loc_1:* = new RadioButton();
				_loc_1.setStyle("skin", ResourceFactory.TabRadioButton_Skin);
				_loc_1.setStyle("skin_up", ResourceFactory.TabRadioButton_Skin_Up);
				_loc_1.setStyle("skin_over", ResourceFactory.TabRadioButton_Skin_Over);
				_loc_1.setStyle("skin_down", ResourceFactory.TabRadioButton_Skin_Down);
				_loc_1.setStyle("skin_sUp", ResourceFactory.TabRadioButton_Skin_Down);
				_loc_1.setStyle("skin_sOver", ResourceFactory.TabRadioButton_Skin_Over);
				_loc_1.setStyle("skin_sDown", ResourceFactory.TabRadioButton_Skin_Down);
				_loc_1.useHandCursor = true;
				_loc_1.clearStyle("iconSkin");
				_loc_1.setStyle("textFormat", tabItemTextFormat);
				_loc_1.setStyle("overTextFormat", tabItemOverTextFormat);
				_loc_1.autoSize = true;
				_loc_1.group = tocGroup;
				_loc_1.value = i;
				var _loc_2:int = -1;
				var _loc_3:int = -1;
				tabBox.addChild(_loc_1);
				var _loc_4:VBox = new VBox();
				_loc_4.setStyle("gap", 8);
				var _loc_5:* = i * tab_delimiter;
				var _loc_6:* = (i + 1) * tab_delimiter;
				var _loc_7:* = _loc_5;
				while (_loc_7 < _loc_6)
				{
					
					if (_loc_7 < _len)
					{
						_loc_8 = trimWhitespaces(convertHTMLEntities(data[_loc_7].title));
						if (_loc_2 == -1)
						{
							_loc_2 = getTitleChapterNumber(_loc_8);
						}
						_loc_9 = new ToCItemButton(_loc_8);
						TooltipManager.getInstance().register(_loc_9, _loc_8);
						_loc_9.addEventListener(MouseEvent.CLICK, tocItemClickHandler);
						_loc_9.useHandCursor = true;
						_loc_9.value = _loc_7;
						tempItemVector.push(_loc_9);
						if (!(_loc_7 & 1))
						{
							_loc_10 = new AutoLayoutContainer();
							_loc_10.width = screenModel.stageWidth - 80;
							_loc_10.addChild(tempItemVector[_loc_7]);
							_loc_10.childsLayoutInfo[tempItemVector[_loc_7]].left = 0;
							_loc_10.childsLayoutInfo[tempItemVector[_loc_7]].widthPercent = 45;
							_loc_4.addChild(_loc_10);
						}
						else
						{
							_loc_10.addChild(tempItemVector[_loc_7]);
							(_loc_10.childsLayoutInfo[tempItemVector[_loc_7]] as LayoutInfo).offsetCenterH = -20;
							(_loc_10.childsLayoutInfo[tempItemVector[_loc_7]] as LayoutInfo).widthPercent = 45;
						}
					}
					_loc_7++;
				}
				_loc_7 = tempItemVector.length - 1;
				while (_loc_7 > _loc_5)
				{
					
					if (_loc_3 == -1)
					{
						_loc_3 = getTitleChapterNumber((tempItemVector[_loc_7] as ToCItemButton).label);
					}
					else
					{
						break;
					}
					_loc_7 = _loc_7 - 1;
				}
				_loc_2 = _loc_2 == -1 ? (i * tab_delimiter + 1) : (_loc_2);
				_loc_3 = _loc_3 == -1 ? ((i + 1) * tab_delimiter > _len ? (_len) : ((i + 1) * tab_delimiter)) : (_loc_3);
				_loc_1.label = StringTemplate.output("第${0}-${1}章", _loc_2, _loc_3);
				tocPages.push(_loc_4);
				return;
			}
				;
			var t:* = getTimer();
			if (!this.tabBox)
			{
				this.addTabBox();
			}
			while (this.tabBox.numChildren)
			{
				
				this.tabBox.removeChildAt(0);
			}
			if (!this.tocContent)
			{
				this.addTocContent();
			}
			this.tocPages = new Vector.<DisplayObject>;
			tocGroup = RadioButtonGroup.getGroup("toc_group");
			tocGroup.addEventListener(Event.CHANGE, this.tabChangeHandler);
			_len = this.tocModel.tocData.length;
			if (_len > LARGE_TOC_THRESHOLD)
			{
				this.tab_delimiter = LARGE_TOC_TAB_DELIMITER;
			}
			else
			{
				this.tab_delimiter = TOC_TAB_DELIMITER;
			}
			tabLen = Math.ceil(_len / this.tab_delimiter);
			data = this.tocModel.tocData;
			tempItemVector = new Vector.<DisplayObject>;
			i;
			this.generateSingleContent();
			tocGroup.selectedData = 0;
			this.tocContent.content = this.tocPages[0];
			(this.tocContent.content as BUI).drawNow();
			this.addEventListener(Event.ENTER_FRAME, function (event:Event) : void
			{
				if (i < (tabLen - 1))
				{
					var _loc_4:* = i + 1;
					i = _loc_4;
					generateSingleContent();
				}
				else
				{
					event.currentTarget.removeEventListener(event.type, arguments.callee);
				}
				return;
			}
			);
			trace("生成目录耗时:", getTimer() - t);
			return;
		}
		
		private function getTitleChapterNumber(param1:String) : int
		{
			var _loc_4:String = null;
			var _loc_5:int = 0;
			var _loc_2:* = /^第([0-9零一二三四五六七八九十百千万]+)./;
			var _loc_3:* = param1.match(_loc_2);
			if (_loc_3 && _loc_3.length >= 2)
			{
				_loc_4 = _loc_3[1];
				if (/[0-9]+/.test(_loc_4))
				{
					_loc_5 = parseInt(_loc_4);
				}
				else
				{
					_loc_5 = Zh2Number.fromString(_loc_4);
				}
				return _loc_5;
			}
			else
			{
				return -1;
			}
		}
		
		private function trimWhitespaces(param1:String) : String
		{
			if (param1 == null)
			{
				return "";
			}
			return param1.replace(/^\s+|\s+$/g, "");
		}
		
		private function tocItemClickHandler(event:MouseEvent) : void
		{
			(event.currentTarget as ToCItemButton).dispatchEvent(new ReaderEvent(ReaderEvent.TOC_GO_TO_PAGE));
			return;
		}
		
		private function addTocContent() : void
		{
			this.tocContent = new ScrollPane();
			this.tocContent.hScrollPolicy = ScrollPolicy.OFF;
			this.tocContent.vScrollBar.setStyle("skin", ResourceFactory.AppScrollBar_Skin);
			this.tocContent.hScrollBar.setStyle("skin", ResourceFactory.AppScrollBar_Skin);
			addChild(this.tocContent);
			var _loc_1:* = this._childsLayoutInfo[this.tocContent];
			_loc_1.top = 100;
			_loc_1.left = 60;
			_loc_1.right = 30;
			_loc_1.bottom = 68;
			return;
		}
		
		private function tabChangeHandler(event:Event) : void
		{
			var _loc_2:* = int((event.target as RadioButtonGroup).selectedData);
			this.tocContent.content = this.tocPages[_loc_2];
			this.tocContent.vScrollPosition = 0;
			(this.tocContent.content as BUI).drawNow();
			return;
		}
		
		private function addTabBox() : void
		{
			this.tabBox = new HBox();
			this.tabBox.setStyle("gap", 20);
			addChild(this.tabBox);
			this.tabBox.height = 22;
			var _loc_1:* = this._childsLayoutInfo[this.tabBox];
			_loc_1.top = 65;
			_loc_1.left = 60;
			_loc_1.right = 60;
			return;
		}
		
		private function addBackButton() : void
		{
			this._backButton = new Button();
			this._backButton.useHandCursor = true;
			this._backButton.setStyle("skin", ResourceFactory.ToCBack_Button_Skin);
			addChild(this._backButton);
			var _loc_1:* = this._childsLayoutInfo[this._backButton] as LayoutInfo;
			_loc_1.top = 25;
			_loc_1.left = 60;
			this._backButton.addEventListener(MouseEvent.CLICK, this.backButtonClickHandler);
			return;
		}
		
		private function backButtonClickHandler(event:MouseEvent) : void
		{
			dispatchEvent(new ReaderEvent(ReaderEvent.GO_BACK_FROM_TOC));
			return;
		}
		
		private function addTocHead() : void
		{
			this.tocHead = new ToCHead();
			addChild(this.tocHead);
			var _loc_1:* = this._childsLayoutInfo[this.tocHead] as LayoutInfo;
			_loc_1.offsetCenterH = 0;
			_loc_1.top = 30;
			return;
		}
		
		public function get backButton() : Button
		{
			return this._backButton;
		}
		
		private function convertHTMLEntities(arg1:String):String
		{
			var loc1:*=["&lt;", "&gt;", "&amp;", "&quot;", "&apos;"];
			var loc2:*=["<", ">", "&", "\"", "\'"];
			var loc3:*=loc1.length;
			while (loc3--) 
			{
				arg1 = arg1.split(loc1[loc3]).join(loc2[loc3]);
			}
			return arg1;
		}
		
	}
}


